#! /bin/sh

# chkconfig: 2345 20 80
# description: Start and stop Voltaire's InfiniBand stack

# Only use ONBOOT option if called by a runlevel directory.
# Therefore determine the base, follow a runlevel link name ...
base=${0##*/}
link=${base#*[SK][0-9][0-9]}
# ... and compare them
if [ $link == $base ] ; then
    RUNMODE=manual
    ONBOOT=yes
else
    RUNMODE=auto
fi

if [ -r /usr/voltaire/gs.conf ]; then
        . /usr/voltaire/gs.conf
fi
if [ -z $start_gvd ] ; then
        start_gvd=y
fi
if [ -z $ipoib_connected ] ; then
        ipoib_connected=n
fi
	
change_openib_conf(){
	if [ -r /etc/infiniband/openib.conf ] ; then
		. /etc/infiniband/openib.conf
		if [ $ipoib_connected = "y" ] ; then
			if [ $SET_IPOIB_CM = "no" ] ; then
				sed -e {s/SET_IPOIB_CM=no/SET_IPOIB_CM=yes/g} /etc/infiniband/openib.conf > /tmp/openib.conf
				cp /tmp/openib.conf /etc/infiniband/openib.conf
			fi
		else
			if [ $SET_IPOIB_CM = "yes" ] ; then
				sed -e {s/SET_IPOIB_CM=yes/SET_IPOIB_CM=no/g} /etc/infiniband/openib.conf > /tmp/openib.conf
				cp /tmp/openib.conf /etc/infiniband/openib.conf
			fi
		fi
	fi
}
			
 
	
ib_his(){
	if [ -x /etc/init.d/ib-his ] ; then
		/etc/init.d/ib-his $1
	fi
}	
ib_enhanced_services(){
	if [ -x /etc/init.d/ib-enhanced-services ] ; then
		/etc/init.d/ib-enhanced-services $1
	fi
}	
openibd(){
	case $1 in
		 start|restart)
			 change_openib_conf
		 ;;
		 *)
		 ;;
	esac
	if [ $RUNMODE = manual ] ; then
		[ -x /etc/init.d/openibd ] && /etc/init.d/openibd $1
	else
		[ -x /usr/voltaire/S00openibd ] && /usr/voltaire/S00openibd $1
	fi
}	
case "$1" in
        start)
		openibd start
		ib_enhanced_services start
		ib_his start
                ;;
        stop)
		ib_enhanced_services stop
		ib_his stop
		if [ -x /usr/bin/ib-bond ] ; then
			/usr/bin/ib-bond --stop-all
		fi
		openibd stop
                ;;
        status)
		openibd status
		ib_enhanced_services status
		ib_his status
                ;;
        restart)
		ib_enhanced_services stop
		ib_his stop
		openibd restart
		ib_enhanced_services start
		ib_his start
                ;;
        *)
                echo "Usage: $0 {start|stop|restart|status}"
                exit 1
                ;;
esac

exit 0

